/*
 * rx_functions.c
 * Created on July 8, 2005; 4:22 PM
 * by VRamadurai
 */
#include <stdio.h>
#include <stdlib.h>
#include <machine/machdefs.h>

/************ Private Header Files **************/
#include "HsTypes.h"
#include "PhyDebug.h"
#include "PhyStruct.h"
#include "PmdGlobals.h"
#include "PhyConstants.h"
#include "iqdma_3500.h"
#include "rx_functions.h"
#include "icrdefs_3500.h"
#include "equalizer.h"

#define MORE_FLAG           1
#define WAIT_CYCLES         0
#define BUSY_WAIT_CYCLES    1
#define SAMPLES_WAIT_CYCLES 0

volatile hs_int32_t atod_status;
volatile hs_uint32_t capture_count;

void sync_on_dma(
                    hs_int32_t  siInitAddrI,
                    hs_int32_t  blocks
                )
{
    hs_int32_t gap;

    hs_uint32_t base = ICR_ADDRESS_CORE_CHANNEL(RX_CORE, MMIO_SB3500A2D_A_OFF); //Core 1, PSDA

    while((((gap=(ICR_GET(base,REG_A2DISTR))-(siInitAddrI & 0x0fffff))< blocks && gap>=0 )  ||
        (gap<0 && (GLOBAL_BUF_SIZE_RX*sizeof(hs_int16_t)+gap) < blocks))
    )
    {
            __sb_barrier();
    }
}

static inline
void set_iq_buffer_ptr(
                        struct HS_Phy11bStr * HS_Phy11bPtrTh,
                        volatile hs_uint32_t iaddr,
                        volatile hs_uint32_t qaddr
                      )
{
    HS_Phy11bPtrTh->piRdPtrI    = (hs_int16_t*)iaddr;
    HS_Phy11bPtrTh->piRdPtrQ    = (hs_int16_t*)qaddr;
}

void stop_rxiqdma()
{
	hs_uint32_t base = ICR_ADDRESS_CORE_CHANNEL(RX_CORE, MMIO_SB3500A2D_A_OFF); //Core1, PSDA

    /** Stop the RF IQ DMA **/
    atod_status = ATOD_STOPPED;
    ICR_SET(base, REG_A2DCTL, 0);
}

void start_iqdma(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t *pssGlobalBufI,
                    hs_int16_t *pssGlobalBufQ
                )
{
    hs_uint32_t iaddr;
    hs_uint32_t qaddr;
    //hs_uint32_t val;

    hs_uint32_t core_base = ICR_ADDRESS_CORE(RX_CORE);
    hs_uint32_t rx_base = ICR_ADDRESS_CORE_CHANNEL(RX_CORE, MMIO_SB3500A2D_A_OFF); // RXREG:Core1, PSDA
    //hs_uint32_t tx_base = ICR_ADDRESS_CORE_CHANNEL(RX_CORE, MMIO_SB3500D2A_A_OFF); // TXREG:Core1, PSDA

    if(atod_status == ATOD_STOPPED){
        iaddr = (hs_uint32_t)pssGlobalBufI;
        qaddr = (hs_uint32_t)pssGlobalBufQ;
        set_iq_buffer_ptr(HS_Phy11bPtrTh, iaddr, qaddr);

        /** Set Direction in UPSD **/
        //val = (0x1000 << 16); //bit28=1, 31=0, 30=0
        //ICR_SET(tx_base, REG_D2ACTL, val);

        /* set I buffer start address */
        ICR_SET(rx_base, REG_A2DISTR, (hs_uint32_t)pssGlobalBufI);
        /* set Q buffer start address */
        ICR_SET(rx_base, REG_A2DQSTR, (hs_uint32_t)pssGlobalBufQ);
        /* set the buffer size and count and start the IQ DMA */
        ICR_SET(rx_base,
                REG_A2DCTL,
                A2D_CTL_ENABLEI
                |A2D_CTL_ENABLEQ
                |A2D_CTL_DECIMATION(0)
                |A2D_CTL_BUFSIZE(SUB_BUF_SIZE_RX-4)
                |A2D_CTL_BUFCOUNT(NSUBFS_RX-1));

        /** Enalbe A2D **/
        ICR_SET(core_base, MMIO_SB3500PSDA_EN, 0x1);

        atod_status = ATOD_RUNNING;
        //printf("the atod_status is %d\n",atod_status); //test code
    }
    else{
        iaddr = ICR_GET(rx_base,REG_A2DISTR) | CORE_ADDR_BASE(1);
        qaddr = ((hs_uint32_t)pssGlobalBufQ + (iaddr - (hs_uint32_t)pssGlobalBufI));
        set_iq_buffer_ptr(HS_Phy11bPtrTh, iaddr, qaddr);
    }
}


void rx_copysamps22_update(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset;
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int32_t i;
    hs_int16_t temp[16];
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;
    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    if((offset=(hs_int32_t)(currPtrI + 22 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        HS_Phy11bPtrTh->piRdPtrI += 22;
        HS_Phy11bPtrTh->piRdPtrQ += 22;

        for(i=0; i<2; i++){
            __sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
        }
    }
    else
    {
        HS_Phy11bPtrTh->piRdPtrI = piStartPtrI + offset;
        HS_Phy11bPtrTh->piRdPtrQ = piStartPtrQ + offset;

        if((22-offset) > 16){
	        __sb_rshra(temp, currPtrI, iq_scale);
	        __sb_rsubf(ssInI, iq_bias, temp);
	        __sb_rshra(temp, currPtrI+16, iq_scale);
	        __sb_rsubf(ssInI+16, iq_bias, temp);
	        __sb_rshra(temp, piStartPtrI, iq_scale);
            __sb_rsubf(ssInI+(22-offset), iq_bias, temp);

            __sb_rshra(temp, currPtrQ, iq_scale);
	        __sb_rsubf(ssInQ, iq_bias, temp);
	        __sb_rshra(temp, currPtrQ+16, iq_scale);
	        __sb_rsubf(ssInQ+16, iq_bias, temp);
	        __sb_rshra(temp, piStartPtrQ, iq_scale);
            __sb_rsubf(ssInQ+(22-offset), iq_bias, temp);
        }
        else{
        	__sb_rshra(temp, currPtrI, iq_scale);
	        __sb_rsubf(ssInI, iq_bias, temp);
	        __sb_rshra(temp, piStartPtrI, iq_scale);
            __sb_rsubf(ssInI+(22-offset), iq_bias, temp);
            __sb_rshra(temp, piStartPtrI+16, iq_scale);
            __sb_rsubf(ssInI+(22-offset)+16, iq_bias, temp);

            __sb_rshra(temp, currPtrQ, iq_scale);
	        __sb_rsubf(ssInQ, iq_bias, temp);
	        __sb_rshra(temp, piStartPtrQ, iq_scale);
            __sb_rsubf(ssInQ+(22-offset), iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16, iq_scale);
            __sb_rsubf(ssInQ+(22-offset)+16, iq_bias, temp);
        }
    }
}

void rx_copysamps44(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset;
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int32_t i;
    hs_int16_t temp[16];
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;

    if((offset=(hs_int32_t)(currPtrI + 44 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        for(i=0; i<3; i++){
            __sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
        }
    }
    else
    {
    	for(i=0; i<((44-offset)&~0xF)>>4; i++){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}
    	if((44-offset) - ((44-offset)&~0xF)){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}

    	for(i=0; i<(offset&~0xF)>>4; i++){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(44-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(44-offset)+16*i, iq_bias, temp);
    	}
    	if(offset - (offset&~0xF)){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(44-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(44-offset)+16*i, iq_bias, temp);
    	}

    	/*
        copy_wrapped_data_44(currPtrI, HS_Phy11bPtrTh->piStartPtrI, ssInI, offset);
        copy_wrapped_data_44(currPtrQ, HS_Phy11bPtrTh->piStartPtrQ, ssInQ, offset);
        */
    }

}


void rx_copysamps16_update(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset,i;
    hs_int16_t temp[16];
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;

    if((offset=(hs_int32_t)(currPtrI + 16 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        HS_Phy11bPtrTh->piRdPtrI += 16;
        HS_Phy11bPtrTh->piRdPtrQ += 16;

        __sb_rshra(ssInI, currPtrI, iq_scale);
        __sb_rsubf(ssInI, iq_bias, ssInI);
        __sb_rshra(ssInQ, currPtrQ, iq_scale);
        __sb_rsubf(ssInQ, iq_bias, ssInQ);

    }
    else
    {
        HS_Phy11bPtrTh->piRdPtrI = HS_Phy11bPtrTh->piStartPtrI + offset;
        HS_Phy11bPtrTh->piRdPtrQ = HS_Phy11bPtrTh->piStartPtrQ + offset;

		__sb_rshra(temp, currPtrI, iq_scale);
        __sb_rsubf(ssInI, iq_bias, temp);
        __sb_rshra(temp, currPtrQ, iq_scale);
        __sb_rsubf(ssInQ, iq_bias, temp);

		__sb_rshra(temp, piStartPtrI, iq_scale);
        __sb_rsubf(ssInI+(16-offset), iq_bias, temp);
        __sb_rshra(temp, piStartPtrQ, iq_scale);
        __sb_rsubf(ssInQ+(16-offset), iq_bias, temp);


    	/*
        copy_wrapped_data_16(currPtrI, HS_Phy11bPtrTh->piStartPtrI, ssInI, offset);
        copy_wrapped_data_16(currPtrQ, HS_Phy11bPtrTh->piStartPtrQ, ssInQ, offset);
        */
    }

}


void rx_copysamps88_update(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset;
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int32_t i;
    hs_int16_t temp[16];
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;

    if((offset=(hs_int32_t)(currPtrI + 88 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        HS_Phy11bPtrTh->piRdPtrI += 88;
        HS_Phy11bPtrTh->piRdPtrQ += 88;

        for(i=0; i<6; i++){
            __sb_rshra(ssInI+i*16, currPtrI+i*16, iq_scale);
            __sb_rsubf(ssInI+i*16, iq_bias, ssInI+i*16);
            __sb_rshra(ssInQ+i*16, currPtrQ+i*16, iq_scale);
            __sb_rsubf(ssInQ+i*16, iq_bias, ssInQ+i*16);
        }
    }
    else
    {
        HS_Phy11bPtrTh->piRdPtrI = HS_Phy11bPtrTh->piStartPtrI + offset;
        HS_Phy11bPtrTh->piRdPtrQ = HS_Phy11bPtrTh->piStartPtrQ + offset;

        for(i=0; i<((88-offset)&~0xF)>>4; i++){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}
    	if((88-offset) - ((88-offset)&~0xF)){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}

    	for(i=0; i<(offset&~0xF)>>4; i++){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(88-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(88-offset)+16*i, iq_bias, temp);
    	}
    	if(offset - (offset&~0xF)){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(88-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(88-offset)+16*i, iq_bias, temp);
    	}

    	/*
        copy_wrapped_data_88(currPtrI, HS_Phy11bPtrTh->piStartPtrI, ssInI, offset);
        copy_wrapped_data_88(currPtrQ, HS_Phy11bPtrTh->piStartPtrQ, ssInQ, offset);
        */
    }

}


void rx_copysamps32_update(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset;
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int32_t i;
    hs_int16_t temp[16];
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;

    if((offset=(hs_int32_t)(currPtrI + 32 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        HS_Phy11bPtrTh->piRdPtrI += 32;
        HS_Phy11bPtrTh->piRdPtrQ += 32;

        __sb_rshra(ssInI, currPtrI, iq_scale);
        __sb_rsubf(ssInI, iq_bias, ssInI);
        __sb_rshra(ssInQ, currPtrQ, iq_scale);
        __sb_rsubf(ssInQ, iq_bias, ssInQ);
        __sb_rshra(ssInI+16, currPtrI+16, iq_scale);
        __sb_rsubf(ssInI+16, iq_bias, ssInI+16);
        __sb_rshra(ssInQ+16, currPtrQ+16, iq_scale);
        __sb_rsubf(ssInQ+16, iq_bias, ssInQ+16);

    }
    else
    {
        HS_Phy11bPtrTh->piRdPtrI = HS_Phy11bPtrTh->piStartPtrI + offset;
        HS_Phy11bPtrTh->piRdPtrQ = HS_Phy11bPtrTh->piStartPtrQ + offset;

        for(i=0; i<((32-offset)&~0xF)>>4; i++){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}
    	if((32-offset) - ((32-offset)&~0xF)){
    		__sb_rshra(temp, currPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+16*i, iq_bias, temp);
            __sb_rshra(temp, currPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+16*i, iq_bias, temp);
    	}

    	for(i=0; i<(offset&~0xF)>>4; i++){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(32-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(32-offset)+16*i, iq_bias, temp);
    	}
    	if(offset - (offset&~0xF)){
    		__sb_rshra(temp, piStartPtrI+16*i, iq_scale);
            __sb_rsubf(ssInI+(32-offset)+16*i, iq_bias, temp);
            __sb_rshra(temp, piStartPtrQ+16*i, iq_scale);
            __sb_rsubf(ssInQ+(32-offset)+16*i, iq_bias, temp);
    	}

    	/*
        copy_wrapped_data_32(currPtrI, HS_Phy11bPtrTh->piStartPtrI, ssInI, offset);
        copy_wrapped_data_32(currPtrQ, HS_Phy11bPtrTh->piStartPtrQ, ssInQ, offset);
        */
    }

}


void rx_copysamps14_update(
                    struct HS_Phy11bStr * HS_Phy11bPtrTh,
                    hs_int16_t * restrict ssInI,
                    hs_int16_t * restrict ssInQ
                )
{
    hs_int32_t offset;
    hs_int16_t *currPtrI;
    hs_int16_t *currPtrQ;
    hs_int16_t temp[16];
    hs_int16_t *iq_scale;
    hs_int16_t *iq_bias;
    hs_int16_t *piStartPtrI;
    hs_int16_t *piStartPtrQ;

    piStartPtrI = HS_Phy11bPtrTh->piStartPtrI;
    piStartPtrQ = HS_Phy11bPtrTh->piStartPtrQ;

    iq_scale = HS_Phy11bPtrTh->iq_scale;
    iq_bias = HS_Phy11bPtrTh->iq_bias;

    currPtrI = HS_Phy11bPtrTh->piRdPtrI;
    currPtrQ = HS_Phy11bPtrTh->piRdPtrQ;

    if((offset=(hs_int32_t)(currPtrI + 14 - HS_Phy11bPtrTh->piEndPtrI)) < 0)
    {
        HS_Phy11bPtrTh->piRdPtrI += 14;
        HS_Phy11bPtrTh->piRdPtrQ += 14;

        __sb_rshra(ssInI, currPtrI, iq_scale);
        __sb_rsubf(ssInI, iq_bias, ssInI);
        __sb_rshra(ssInQ, currPtrQ, iq_scale);
        __sb_rsubf(ssInQ, iq_bias, ssInQ);

    }
    else
    {
        HS_Phy11bPtrTh->piRdPtrI = HS_Phy11bPtrTh->piStartPtrI + offset;
        HS_Phy11bPtrTh->piRdPtrQ = HS_Phy11bPtrTh->piStartPtrQ + offset;

        __sb_rshra(temp, currPtrI, iq_scale);
        __sb_rsubf(ssInI, iq_bias, temp);
        __sb_rshra(temp, currPtrQ, iq_scale);
        __sb_rsubf(ssInQ, iq_bias, temp);

		__sb_rshra(temp, piStartPtrI, iq_scale);
        __sb_rsubf(ssInI+(14-offset), iq_bias, temp);
        __sb_rshra(temp, piStartPtrQ, iq_scale);
        __sb_rsubf(ssInQ+(14-offset), iq_bias, temp);

        /*
        copy_wrapped_data_14(currPtrI, HS_Phy11bPtrTh->piStartPtrI, ssInI, offset);
        copy_wrapped_data_14(currPtrQ, HS_Phy11bPtrTh->piStartPtrQ, ssInQ, offset);
        */
    }

}


/** This has to be called for every L1_rx() **/
void init_rx(struct HS_Phy11bStr *HS_Phy11bPtrTh)
{

	atod_status = ATOD_STOPPED;

    *HS_Phy11bPtrTh->rx_status_l = HS_INCOMPLETE;
    HS_Phy11bPtrTh->siClipFlag = 0;
    HS_Phy11bPtrTh->preamble_syncbit_pos = 0;
    HS_Phy11bPtrTh->siRotorTableCounter = 0;
    HS_Phy11bPtrTh->siNPayloadSym = 0;
    HS_Phy11bPtrTh->siAvgDiffSymbI = 0;
    HS_Phy11bPtrTh->siAvgDiffSymbQ = 0;
    HS_Phy11bPtrTh->ucDescrState  = 0;
    HS_Phy11bPtrTh->e_detected = 0;
    HS_Phy11bPtrTh->data_decoding = 0;
    capture_count = 0;
}

void init_global_buffers(struct HS_Phy11bStr * HS_Phy11bPtrTh)
{
    /*setting the initial and final address of the global buffers*/
    HS_Phy11bPtrTh->piStartPtrI = (hs_int16_t *)( &GPHY_asInputI[0]);
    HS_Phy11bPtrTh->piEndPtrI   = (hs_int16_t *)( &GPHY_asInputI[GLOBAL_BUF_SIZE_RX]);
    HS_Phy11bPtrTh->piStartPtrQ = (hs_int16_t *)( &GPHY_asInputQ[0]);
}

void capture_all_iqdata_rx_samps(int samps)
{
    FILE *fp_i;
    int i;

    fp_i = fopen("i_out1.txt", "w");

    for(i=0; i<samps; i++){
        fprintf(fp_i, "%d %d\n", GPHY_asInputI[i], GPHY_asInputQ[i]);
    }

    fclose(fp_i);
}

void capture_all_iqdata_rx()
{
    FILE *fp_i;
    FILE *fp_q;
    int i;

    fp_i = fopen("i_out1.txt", "w");
    fp_q = fopen("q_out1.txt", "w");

    for(i=0; i<GLOBAL_BUF_SIZE_RX; i++){
        //fprintf(fp_i, "%d %d\n", IQ_SCALE(GPHY_asInputI[i]), IQ_SCALE(GPHY_asInputQ[i]));
        fprintf(fp_i, "%d %d\n", ((unsigned short)(GPHY_asInputI[i]) >> 6) - 512,
		((unsigned short)(GPHY_asInputQ[i]) >> 6) - 512);
    }

    fclose(fp_i);
    fclose(fp_q);
}





